﻿#if DebugTest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Ivan.Dm.ChromeRemoteDebugger.Transport;
using System.IO;
using System.Threading;

namespace Ivan.Dm.ChromeRemoteDebugger.Tests.Transport {
    [TestFixture]
    public class TestMessage {
        [Test]
        public void WriteToStream() {
            string strExpectedMessage =
                "Tool:someTool\r\n" +
                "Destination:someDestination\r\n" +
                "Content-Length:11\r\n\r\n" +
                "someContent";
            Dictionary<string, string> header = new Dictionary<string, string>();
            header.Add(MessageHeaderParts.ToolEntryKey, "someTool");
            header.Add(MessageHeaderParts.DestintaionEntryKey, "someDestination");
            Message message = new Message(header, "someContent");
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        message.WriteToStream(writer);
                        stream.Seek(0, SeekOrigin.Begin);
                        Assert.AreEqual(strExpectedMessage, reader.ReadToEnd());
                    }
                }
            }
        }

        [Test]
        public void FromStream() {
            string strMessage =
                "Tool:someTool\r\n" +
                "Destination:someDestination\r\n" +
                "Content-Length:11\r\n\r\n" +
                "someContent";
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        writer.Write(strMessage);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        Message message = null;
                        message = Message.ReadFromStream(reader);
                        Assert.AreEqual("someDestination", message.Destination);
                        Assert.AreEqual("someTool", message.Tool);
                        Assert.AreEqual("someContent", message.Content);
                    }
                }
            }
        }

        [Test]
        public void FromByteArray_NoHeader() {
            string strMessage = "someContent";
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        writer.Write(strMessage);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        Message message = null;
                        message = Message.ReadFromStream(reader);
                        Assert.IsNull(message);
                    }
                }
            }
        }

        [Test]
        public void FromByteArray_InvalidHeader() {
            string strMessage =
                "Tool:someTool\r\n" +
                "DestinationsomeDestination\r\n" +
                "Content-Length:11\r\n\r\n" +
                "someContent";
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        writer.Write(strMessage);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        Message message = null;
                        message = Message.ReadFromStream(reader);
                        Assert.IsNull(message);
                    }
                }
            }
        }

        [Test]
        public void FromByteArray_CorruptedContentLengthInHeader() {
            string strMessage =
                "Tool:someTool\r\n" +
                "Destination:someDestination\r\n" +
                "Content-Length:abc\r\n\r\n" +
                "someContent";
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        writer.Write(strMessage);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        Message message = null;
                        message = Message.ReadFromStream(reader);
                        Assert.IsNull(message);
                    }
                }
            }
        }

        [Test]
        public void FromByteArray_InvalidContentLength() {
            string strMessage =
                "Tool:someTool\r\n" +
                "Destination:someDestination\r\n" +
                "Content-Length:11\r\n\r\n" +
                "someCon";
            using (var stream = new MemoryStream()) {
                using (var writer = new StreamWriter(stream)) {
                    using (var reader = new StreamReader(stream)) {
                        writer.Write(strMessage);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        Message message = null;
                        message = Message.ReadFromStream(reader);
                        Assert.IsNull(message);
                    }
                }
            }
        }
    }
}
#endif